<style lang="less">
    @import '../../styles/loading.less';
</style>

<template>
    <div>
        <Col span="12">
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> {{pig_id?'编辑':'新增'}}土猪
                </p>
                <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                    <Form-item label="农场" prop="farm_id">
                        <Select v-model="formValidate.farm_id" style="width:200px">
                            <Option v-for="item in farmList" :key="item.farm_id" :value="item.farm_id" >{{item.name}}</Option>
                        </Select>
                    </Form-item>
                    <Form-item label="名称" prop="name">
                        <Input v-model="formValidate.name" placeholder="Enter..."></Input>
                    </Form-item>
                    <Form-item label="耳标" prop="ear_mark">
                        <Input v-model="formValidate.ear_mark" placeholder="Enter..."></Input>
                    </Form-item>
                    <Form-item label="编码" prop="pig_code">
                        <Input v-model="formValidate.pig_code" placeholder="Enter..."></Input>
                    </Form-item>
                    <Form-item label="性别" prop="sex">
                        <Radio-group v-model="formValidate.sex">
                            <Radio label="1">公</Radio>
                            <Radio label="2">母</Radio>
                        </Radio-group>
                    </Form-item>
                    <Form-item label="图片" prop="cover_img">
                        <img :src="formValidate.cover_img+'?x-oss-process=image/resize,h_100'" v-if="formValidate.cover_img" style="margin: 10px;">
                        <uploadImg v-on:uploadImg="uploadImg"></uploadImg>
                    </Form-item>
                    <Form-item label="进栏日期" prop="enter_date">
                        <Date-picker type="date" v-model="check_date" @on-change="formatDate" placeholder="选择日期" style="width: 200px"></Date-picker>
                    </Form-item>
                    <Form-item label="进栏重量(kg)" prop="enter_weight">
                        <Input v-model="formValidate.enter_weight" placeholder="Enter..."></Input>
                    </Form-item>

                    <Form-item>
                        <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                        <Button type="ghost" v-if="!pig_id" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                    </Form-item>
                </Form>
            </Card>
        </Col>
        <Col span="12">
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> 打预防
                </p>
                <Form ref="injectForm" :model="injectForm" :rules="injectValidate" :label-width="80">
                    <Form-item label="预防记录">
                        {{formValidate.inject_date}}
                    </Form-item>
                    <div v-if="formValidate.outer_date=='' || formValidate.outer_weight==0">
                        <Form-item label="时间" prop="inject_date">
                            <Date-picker type="date" v-model="check_inject_date" @on-change="formatDate" placeholder="选择日期" style="width: 200px"></Date-picker>
                        </Form-item>
                        <Form-item>
                            <Button type="primary" @click="handleInject('injectForm')">提交</Button>
                        </Form-item>
                    </div>
                </Form>
            </Card>
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> 出栏
                </p>
                <Form ref="outerForm" :model="outerForm" :rules="outerValidate" :label-width="80">
                    <div v-if="formValidate.outer_date!='' || formValidate.outer_weight!=0">
                        <Form-item label="出栏" prop="outer">
                            时间:{{formValidate.outer_date}}，重量:{{formValidate.outer_weight}}kg
                        </Form-item>
                    </div>
                    <div v-else>
                        <Form-item label="出栏时间" prop="outer_date">
                            <Date-picker type="date" v-model="check_outer_date" @on-change="formatDate" placeholder="选择日期" style="width: 200px"></Date-picker>
                        </Form-item>
                        <Form-item label="出栏重量(kg)" prop="outer_weight">
                            <Input v-model="outerForm.outer_weight" placeholder="Enter..."></Input>
                        </Form-item>
                        <Form-item>
                            <Button type="primary" @click="handleOuter('outerForm')">提交</Button>
                        </Form-item>
                    </div>
                </Form>
            </Card>
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> 加入预售
                </p>
                <Form :label-width="80">
                    <Form-item label="预售状态" prop="name">
                        {{pre_sale_status}}
                    </Form-item>
                    <Form-item v-if="formValidate.status==0 && formValidate.outer_date==''">
                        <Button type="primary" @click="handlePreSale()">加入预售</Button>
                    </Form-item>
                </Form>
            </Card>
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> 添加监控
                </p>
                <Form ref="webcamForm" :model="webcamForm" :rules="webcamValidate" :label-width="80">
                    <div v-if="formValidate.webcam_id">
                        <Form-item label="监控ID">
                            {{formValidate.webcam_id}}
                        </Form-item>
                    </div>
                    <div v-else>
                        <Form-item label="选择监控" prop="webcam_id">
                            <Select v-model="webcamForm.webcam_id">
                                <Option v-for="item in webcamList" :key="item.webcam_id" :value="item.webcam_id" >{{item.webcam_id+' '+item.province+item.city+item.district+item.full_address}}</Option>
                            </Select>
                        </Form-item>
                        <Form-item>
                            <Button type="primary" @click="handleWebcam('webcamForm')">提交</Button>
                        </Form-item>
                    </div>
                </Form>
            </Card>
        </Col>
    </div>
</template>

<script>
    import uploadImg from '../website/upload';
    export default {
        components:{
            'uploadImg':uploadImg,
        },
        data () {
            return {
                pig_id: '',
                check_date: '',
                farmList: [],
                formValidate: {
                    farm_id: 0,
                    name: '',
                    ear_mark: '',
                    pig_code: '',
                    cover_img: '',
                    enter_date: '',
                    enter_weight: '',
                    sex: 0,
                    inject_date: '',
                    outer_date: '',
                    outer_weight: null,
                    status: 0,
                    webcam_id: 0,
                },
                ruleValidate: {
                    farm_id: [
                        { required: true, message: '请选择农场' },
                    ],
                    name: [
                        { required: true, message: '名称不能为空' },
                        { type: 'string', max: 20, message: '最多为20位' },
                    ],
                    ear_mark: [
                        { required: true, message: '请输入耳标' },
                    ],
                    pig_code: [
                        { required: true, message: '请输入编号' },
                    ],
                    enter_date: [
                        { required: true, message: '请输入进栏时间' },
                    ],
                    enter_weight: [
                        { required: true, message: '请输入进栏重量(kg)' },
                    ],
                },

                check_inject_date: '',
                injectForm: {
                    inject_date: '',
                },
                injectValidate: {
                    inject_date: [
                        { required: true, message: '请输入打预防时间' },
                    ],
                },

                check_outer_date: '',
                outerForm: {
                    outer_date: '',
                    outer_weight: '',
                },
                outerValidate: {
                    outer_date: [
                        { required: true, message: '请输出栏时间' },
                    ],
                    outer_weight: [
                        { required: true, message: '请输入出栏重量' },
                    ],
                },

                pre_sale_status: '正常',

                webcamList: [],
                webcamForm: {
                    webcam_id: '0',
                },
                webcamValidate: {
                    webcam_id: [
                        { required: true, message: '请选择监控ID' },
                    ],
                },
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let url = this.pig_id ? 'admin/pig/edit' : 'admin/pig/add';
                        let postData = this.formValidate;
                        postData.pig_id = this.pig_id; console.log(postData);
                        this.$http.post(url, this.$qs.stringify(postData)).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                this.$router.push({
                                    name: 'pigList'
                                });
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            uploadImg (data) {
                console.log(data);
                this.formValidate.cover_img = data;
            },
            formatDate (date) {
                this.formValidate.enter_date = date;
                this.injectForm.inject_date = date;
                this.outerForm.outer_date = date;
            },
            getOne () {
                this.$http.post('admin/pig/getOne', this.$qs.stringify({pig_id: this.pig_id})).then((res) => {
                    var d = res.data;
                    if (d.code == 0) {
                        this.formValidate = {
                            farm_id: d.data.farm_id,
                            name: d.data.name,
                            ear_mark: d.data.ear_mark,
                            pig_code: d.data.pig_code,
                            cover_img: d.data.cover_img,
                            enter_date: d.data.enter_date,
                            enter_weight: d.data.enter_weight,
                            sex: d.data.sex,
                            inject_date: d.data.inject_date,
                            outer_date: d.data.outer_date,
                            outer_weight: d.data.outer_weight,
                            status: d.data.status,
                            webcam_id: d.data.webcam_id,
                        };
                        this.check_date = d.data.enter_date;
                        switch (d.data.status) {//预售状态
                            case 0 : this.pre_sale_status = '正常';break;
                            case 1 : this.pre_sale_status = '待预售';break;
                            case 2 : this.pre_sale_status = '部分已售';break;
                            case 3 : this.pre_sale_status = '全部预售';break;
                            case 4 : this.pre_sale_status = '已宰杀';break;
                        }
                        if (d.data.webcam_id==0) {//如果没有监控
                            this.getWebcam();
                        }
                    } else {
                        this.$Notice.error({
                            title: 'Error Warning',
                            desc: `Error：${d.msg}<br>`
                        });
                    }
                });
            },
            getFarm () {
                this.$http.post('admin/pig/getFarm', this.$qs.stringify()).then((res) => {
                    var d = res.data;
                    if (d.code == 0) {
                        this.farmList = d.data;
                    } else {
                        this.$Notice.error({
                            title: 'Error Warning',
                            desc: `Error：${d.msg}<br>`
                        });
                    }
                });
            },
            handleInject (name) {//打预防
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let postData = this.injectForm;
                        postData.pig_id = this.pig_id; console.log(postData);
                        this.$http.post('admin/pig/doPreInject', this.$qs.stringify(postData)).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                location.reload();
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleOuter (name) {//出栏
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let postData = this.outerForm;
                        postData.pig_id = this.pig_id; console.log(postData);
                        this.$http.post('admin/pig/doOuter', this.$qs.stringify(postData)).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                location.reload();
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handlePreSale () {//预售
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认将土猪加入预售？',
                    onOk: () => {
                        this.$http.post('admin/pig/doPreSale', {pig_id:this.pig_id}).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                location.reload();
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    },
                });
            },
            handleWebcam (name) {//添加监控
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let postData = this.webcamForm;
                        postData.pig_id = this.pig_id; console.log(postData);
                        this.$http.post('admin/pig/doWebcam', this.$qs.stringify(postData)).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                location.reload();
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            getWebcam () {//获取监控
                this.$http.post('admin/webcam/getList').then((res) => {
                    var d = res.data;
                    if (d.code == 0) {
                        this.webcamList = d.data;
                    } else {
                        this.$Notice.error({
                            title: 'Error Warning',
                            desc: `Error：${d.msg}<br>`
                        });
                    }
                });
            }
        },
        mounted() {
            this.$nextTick(() => {
                this.getFarm();

                this.pig_id = this.$route.query.pig_id;
                if (this.pig_id) {
                    this.getOne();
                }
            });
        },
    };
</script>
